# This package will contain the spiders of your Scrapy project
#
# To create the first spider for your project use this command:
#
#   scrapy genspider myspider myspider-domain.com
#
# For more info see:
# http://doc.scrapy.org/topics/spiders.html

import re

import MySQLdb
from _mysql_exceptions import MySQLError

try:
    conn = MySQLdb.connect(host = "localhost", user = "root", passwd = "", db = "akademik_kateglo")
except MySQLError, e:
    print "Oops, cannot connect to database. Error %d: %s" % (e.args[0], e.args[1])
    exit()

def removeNonAscii(s): return "".join(i for i in s if ord(i)<128)

def removeHtmlTags(s): return re.sub('<[^<^>]*>', '', s)

def sanitize(s): return removeHtmlTags(removeNonAscii(s.replace(u'\u2192 ', 'see: ')))

def arrToStr(arr): return ''.join(arr)

def get_dictionary_item():
    cursor = conn.cursor()
    query = """
        SELECT id, phrase
        FROM dictionary
        WHERE is_detailed = 0
    """
    cursor.execute(query)
    result = cursor.fetchone()
    return result